package SwingView.Quiz;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import SwingController.CmsPresenter;
import SwingModel.CmsQuiz;
import SwingView.CmsContentPanelModel;


public class CmsCPQuizMainInstructor extends CmsContentPanelModel{

	private final JLabel jlTitle;
	private final JPanel jpContent;
	private final JButton jbtNewQuiz;
	
	public CmsCPQuizMainInstructor(){
		
		
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(600,65));
		jp.setOpaque(false);
		jp.setLayout( new GridBagLayout());
		jp.setBorder(BorderFactory.createEmptyBorder());
		
		//instantiate People title
		jlTitle = new JLabel("Quiz");
		jlTitle.setFont(jlTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlTitle.setAlignmentY(CENTER_ALIGNMENT);
		jlTitle.setAlignmentY(CENTER_ALIGNMENT);
		jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 8, 10, 8));
		GridBagConstraints c = new GridBagConstraints();
		c.gridy = 0;
		c.anchor = GridBagConstraints.CENTER;
		jp.add(jlTitle,c);
		
		//new quiz button
		jbtNewQuiz = new JButton ("+ Post new Quiz");
		jbtNewQuiz.setFont(jbtNewQuiz.getFont().deriveFont(13.0f).deriveFont(Font.BOLD));
		jbtNewQuiz.setForeground(Color.BLUE);
		jbtNewQuiz.setOpaque(false);
		jbtNewQuiz.setCursor(new Cursor(Cursor.HAND_CURSOR));
		jbtNewQuiz.setBorder(BorderFactory.createEmptyBorder(0,8,0,8));
		c = new GridBagConstraints();
		c.gridy = 1;
		c.insets = new Insets(0, 0, 10, 0);
		c.anchor = GridBagConstraints.LINE_START;
		c.weightx = 1.0;
		c.weighty = 1.0;
		jp.add(jbtNewQuiz,c);
		jbtNewQuiz.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				CmsPresenter.mainView.displayQuizComposerPage();
			}
			
		});
		
		//content panel
		jpContent = new JPanel();
		jpContent.setLayout(new GridBagLayout());
		jpContent.setOpaque(false);
		
		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);
		
		//set up student lecture panel and add components
		setLayout(new BorderLayout());
		add(jp,BorderLayout.NORTH);//Add title People
		add(scroll,BorderLayout.WEST);//Add People List
	}

	
	public void displayListOfQuiz(List<CmsQuiz> listOfQuiz){
		jpContent.removeAll();
		jpContent.revalidate();
		jpContent.repaint();
		int i = 0;
		for(CmsQuiz quiz : listOfQuiz){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			c.insets = new Insets(0, 0, 8, 0);
			jpContent.add(new QuizEntryRow(quiz),c);
			i++;
		}
		
		//empty space at bottom - used to fill the bottom of the page
		//when the list is too short
		JPanel p = new JPanel();
		p.setOpaque(false);
		p.setBackground(Color.WHITE);
		GridBagConstraints c_p = new GridBagConstraints();
		c_p.gridy = i;
		c_p.fill = GridBagConstraints.VERTICAL;
		c_p.weightx=1.0;
		c_p.weighty=1.0;
		jpContent.add(p,c_p);
		
		jpContent.repaint();
		jpContent.revalidate();
	}
	
	public class QuizEntryRow extends JPanel{
		
		
		final static private int ROW_WIDTH = 550;
		final static private int ROW_HEIGHT = 80;
		
		final static private int DATE_NAME_COLUMN_SIZE = 100;
		private JButton viewButton;
		private JButton editButton;
		private JButton detailButton;
		private JButton deleteButton;
		private JPanel jpQuizContent;
		private CmsQuiz quiz;
		private JButton subButton;
		
		public QuizEntryRow(CmsQuiz quiz){
			this.quiz = quiz;
			
			JLabel jlTitle;
			JLabel postedDate; //avai date
			JLabel pdTitle;
			JLabel expDate; //exp date
			JLabel edTitle;
			
			
			//Set up file name label
			String quizTitle = quiz.getQuizTitle();
			jlTitle = new JLabel(quizTitle);
			jlTitle.setFont(jlTitle.getFont().deriveFont(15.0f).deriveFont(Font.BOLD));
			jlTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 5, 5));
			
			//Set up the "Available date" title label
			pdTitle = new JLabel("Posted Date: ");
			pdTitle.setFont(pdTitle.getFont().deriveFont(10.0f).deriveFont(Font.BOLD));
			pdTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 0, 5));
			
			//set up avai date Label
			postedDate = new JLabel(CmsPresenter.sdf.format(new Date(quiz.getCreatedDate().getTime())));
			postedDate.setFont(jlTitle.getFont().deriveFont(10.0f));
			postedDate.setForeground(new Color(40,40,40));
			postedDate.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
			
			//set up view button
			viewButton = new JButton ("View");
			viewButton.setFont(viewButton.getFont().deriveFont(12.0f));
			viewButton.setForeground(Color.BLUE);
			viewButton.setOpaque(false);
			viewButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			viewButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//set up delete button
			deleteButton = new JButton ("Delete");
			deleteButton.setFont(viewButton.getFont().deriveFont(12.0f));
			deleteButton.setForeground(Color.BLUE);
			deleteButton.setOpaque(false);
			deleteButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			deleteButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//set up edit button
			editButton = new JButton ("Edit");
			editButton.setFont(viewButton.getFont().deriveFont(12.0f));
			editButton.setForeground(Color.BLUE);
			editButton.setOpaque(false);
			editButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			editButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//set up delete button
			detailButton = new JButton ("Show Details");
			detailButton.setFont(viewButton.getFont().deriveFont(12.0f));
			detailButton.setForeground(Color.BLUE);
			detailButton.setOpaque(false);
			detailButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			detailButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//set up delete button
			subButton = new JButton ("Student Submissions");
			subButton.setFont(subButton.getFont().deriveFont(12.0f));
			subButton.setForeground(Color.BLUE);
			subButton.setOpaque(false);
			subButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			subButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//Set up the panel
			setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setBackground(Color.WHITE);
			setBorder(BorderFactory.createLineBorder(new Color(170,170,170), 1));
			setOpaque(true);
			setLayout(new GridBagLayout());
			
			//add the Title
			GridBagConstraints cf = new GridBagConstraints();
			cf.fill = GridBagConstraints.HORIZONTAL;
			cf.anchor = GridBagConstraints.FIRST_LINE_START;
			cf.gridwidth = 4;
			add(jlTitle,cf);
			
			//add "Posted date" title:
			GridBagConstraints cp = new GridBagConstraints();
			cp.insets = new Insets(0,0,10,0);
			cp.gridx = 0;
			cp.gridy = 1;
			cp.anchor = GridBagConstraints.LAST_LINE_START;
			add(pdTitle,cp);
			
			//add the posted date
			GridBagConstraints cd = new GridBagConstraints();
			cd.insets = new Insets(0,0,10,0);
			cd.gridx = 1;
			cd.gridy = 1;
			cd.anchor = GridBagConstraints.LAST_LINE_START;
			add(postedDate,cd);
			
			
			//add the view button
			GridBagConstraints cb = new GridBagConstraints();
			cb.gridx = 1;
			cb.gridy = 2;
			cb.weightx = 1.0;
			cb.anchor = GridBagConstraints.LAST_LINE_END;
			add(viewButton,cb);
			
			//add the edit button
			GridBagConstraints cdel = new GridBagConstraints();
			cdel.gridx = 2;
			cdel.gridy = 2;
			cdel.anchor = GridBagConstraints.LAST_LINE_END;
			add(editButton,cdel);
			
			//add the edit button
			cdel = new GridBagConstraints();
			cdel.gridx = 3;
			cdel.gridy = 2;
			cdel.anchor = GridBagConstraints.LAST_LINE_END;
			add(subButton,cdel);
			
			//add the delete button
			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 4;
			c.gridy = 2;
			c.anchor = GridBagConstraints.LAST_LINE_END;
			add(deleteButton,c);
			
			//add the delete button
			c = new GridBagConstraints();
			c.gridx = 5;
			c.gridy = 2;
			c.anchor = GridBagConstraints.LAST_LINE_END;
			add(detailButton,c);
			
			jpQuizContent = new JPanel();
			jpQuizContent.setOpaque(false);
			c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = 3;
			c.gridwidth = 6;
			c.weighty = 1.0;
			c.insets = new Insets(0,10,10,10);
			c.fill = GridBagConstraints.VERTICAL;
			c.anchor = GridBagConstraints.FIRST_LINE_START;
			add(jpQuizContent,c);
			
			//Add View AL:
			viewButton.addActionListener(new ALQuizView(quiz));
			deleteButton.addActionListener(new ALQuizDelete(quiz));
			editButton.addActionListener(new ALQuizEdit(quiz));
			detailButton.addActionListener(new ShowDetailActionListener());
			subButton.addActionListener(new ALViewQuizSubmission(quiz.getQuizID()));
		}
		

		
		public class ShowDetailActionListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				if(detailButton.getText().equals("Show Details")){
					detailButton.setText("Hide Details");
					
					GridBagLayout gridBagLayout = new GridBagLayout();
					jpQuizContent.setLayout(gridBagLayout);
					jpQuizContent.setOpaque(false);
					
					JLabel lblAvailableDate = new JLabel("Available Date: ");
					lblAvailableDate.setFont(new Font("Dialog", Font.BOLD, 12));
					GridBagConstraints gbc_lblAvailableDate = new GridBagConstraints();
					gbc_lblAvailableDate.anchor = GridBagConstraints.LINE_START;
					gbc_lblAvailableDate.insets = new Insets(10, 20, 5, 5);
					gbc_lblAvailableDate.gridx = 0;
					gbc_lblAvailableDate.gridy = 0;
					jpQuizContent.add(lblAvailableDate, gbc_lblAvailableDate);
					
					JLabel jlbAvaiDate = new JLabel();
					Date avaiDate = new Date(quiz.getAvaiDate().getTime());
					if(avaiDate.compareTo(CmsPresenter.DEFAULT_AVAI_DATE) == 0){
						jlbAvaiDate.setText("<NONE>");
					}
					else jlbAvaiDate.setText(CmsPresenter.sdf.format(avaiDate));
					GridBagConstraints gbc_jlbAvaiDate = new GridBagConstraints();
					gbc_jlbAvaiDate.anchor = GridBagConstraints.LINE_START;
					gbc_jlbAvaiDate.insets = new Insets(10, 0, 5, 5);
					gbc_jlbAvaiDate.gridx = 1;
					gbc_jlbAvaiDate.gridy = 0;
					jpQuizContent.add(jlbAvaiDate, gbc_jlbAvaiDate);
					
					JLabel lblNumberOfQuestions = new JLabel("Number Of Questions: ");
					lblNumberOfQuestions.setFont(new Font("Lucida Grande", Font.BOLD, 13));
					GridBagConstraints gbc_lblNumberOfQuestions = new GridBagConstraints();
					gbc_lblNumberOfQuestions.anchor = GridBagConstraints.LINE_START;
					gbc_lblNumberOfQuestions.insets = new Insets(0, 20, 5, 5);
					gbc_lblNumberOfQuestions.gridx = 0;
					gbc_lblNumberOfQuestions.gridy = 3;
					jpQuizContent.add(lblNumberOfQuestions, gbc_lblNumberOfQuestions);
					
					JLabel jlbQNum = new JLabel("" + ((int) quiz.getMaxPoint()));
					GridBagConstraints gbc_jlbQNum = new GridBagConstraints();
					gbc_jlbQNum.anchor = GridBagConstraints.LINE_START;
					gbc_jlbQNum.insets = new Insets(0, 0, 5, 5);
					gbc_jlbQNum.gridx = 1;
					gbc_jlbQNum.gridy = 3;
					jpQuizContent.add(jlbQNum, gbc_jlbQNum);
					
					JLabel lblDueDate = new JLabel("Due Date: ");
					lblDueDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
					GridBagConstraints gbc_lblDueDate = new GridBagConstraints();
					gbc_lblDueDate.anchor = GridBagConstraints.LINE_START;
					gbc_lblDueDate.insets = new Insets(0, 20, 5, 5);
					gbc_lblDueDate.gridx = 0;
					gbc_lblDueDate.gridy = 1;
					jpQuizContent.add(lblDueDate, gbc_lblDueDate);
					
					JLabel jlbDueDate = new JLabel(CmsPresenter.sdf.format(new Date(quiz.getDueDate().getTime())));
					GridBagConstraints gbc_jlbDueDate = new GridBagConstraints();
					gbc_jlbDueDate.anchor = GridBagConstraints.LINE_START;
					gbc_jlbDueDate.insets = new Insets(0, 0, 5, 5);
					gbc_jlbDueDate.gridx = 1;
					gbc_jlbDueDate.gridy = 1;
					jpQuizContent.add(jlbDueDate, gbc_jlbDueDate);
					
					JLabel lblPercentWeight = new JLabel("Percent Weight: ");
					lblPercentWeight.setFont(new Font("Lucida Grande", Font.BOLD, 13));
					GridBagConstraints gbc_lblPercentWeight = new GridBagConstraints();
					gbc_lblPercentWeight.anchor = GridBagConstraints.LINE_START;
					gbc_lblPercentWeight.insets = new Insets(0, 20, 5, 5);
					gbc_lblPercentWeight.gridx = 0;
					gbc_lblPercentWeight.gridy = 4;
					jpQuizContent.add(lblPercentWeight, gbc_lblPercentWeight);
					
					JLabel jlbWeight = new JLabel("" + quiz.getPercentWeight() + "%");
					GridBagConstraints gbc_jlbWeight = new GridBagConstraints();
					gbc_jlbWeight.anchor = GridBagConstraints.LINE_START;
					gbc_jlbWeight.insets = new Insets(0, 0, 5, 5);
					gbc_jlbWeight.gridx = 1;
					gbc_jlbWeight.gridy = 4;
					jpQuizContent.add(jlbWeight, gbc_jlbWeight);
					
					JLabel lblExpirationDate = new JLabel("Expiration Date: ");
					lblExpirationDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
					GridBagConstraints gbc_lblExpirationDate = new GridBagConstraints();
					gbc_lblExpirationDate.anchor = GridBagConstraints.LINE_START;
					gbc_lblExpirationDate.insets = new Insets(0, 20, 5, 5);
					gbc_lblExpirationDate.gridx = 0;
					gbc_lblExpirationDate.gridy = 2;
					jpQuizContent.add(lblExpirationDate, gbc_lblExpirationDate);
					
					JLabel jlbExpDate = new JLabel();
					Date expDate = new Date(quiz.getExpDate().getTime());
					if(expDate.compareTo(CmsPresenter.DEFAULT_EXP_DATE) == 0){
						jlbExpDate.setText("<NONE>");
					}
					else jlbExpDate.setText(CmsPresenter.sdf.format(expDate));
					GridBagConstraints gbc_jlbExpDate = new GridBagConstraints();
					gbc_jlbExpDate.anchor = GridBagConstraints.LINE_START;
					gbc_jlbExpDate.insets = new Insets(0, 0, 5, 5);
					gbc_jlbExpDate.gridx = 1;
					gbc_jlbExpDate.gridy = 2;
					jpQuizContent.add(jlbExpDate, gbc_jlbExpDate);
					
					JLabel lblTimeConstraint = new JLabel("Time Constraint: ");
					lblTimeConstraint.setFont(new Font("Lucida Grande", Font.BOLD, 13));
					GridBagConstraints gbc_lblTimeConstraint = new GridBagConstraints();
					gbc_lblTimeConstraint.anchor = GridBagConstraints.LINE_START;
					gbc_lblTimeConstraint.insets = new Insets(0, 20, 10, 5);
					gbc_lblTimeConstraint.gridx = 0;
					gbc_lblTimeConstraint.gridy = 5;
					jpQuizContent.add(lblTimeConstraint, gbc_lblTimeConstraint);
					
					
					
					JLabel jlbTime = new JLabel("" + quiz.getDuration() + " minutes");
					if(quiz.getDuration() == 0) jlbTime.setText("None");
					GridBagConstraints gbc_jlbTime = new GridBagConstraints();
					gbc_jlbTime.anchor = GridBagConstraints.LINE_START;
					gbc_jlbTime.insets = new Insets(0, 0, 10, 5);
					gbc_jlbTime.gridx = 1;
					gbc_jlbTime.gridy = 5;
					jpQuizContent.add(jlbTime, gbc_jlbTime);
					
					
					setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT+160));
					setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT+160));
					
					jpQuizContent.revalidate();
				}
				else{
					detailButton.setText("Show Details");
					jpQuizContent.removeAll();
					setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
					setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
					setBackground(Color.WHITE);
					jpQuizContent.revalidate();
				}
				
			}
			
		}
	}
	
}
